import GameMgr from "./gameMgr";


interface myNode extends cc.Node {
    index?: number
    coverArr?: any[]
}


export default class utils {
    /**
     * @param node 外部节点
     * @param target 绑定到谁身上
     * @param component 绑定的哪个组件的代码
     * @param callback 回调函数
     * 
     */

    addClickEvent(node: cc.Node, target: cc.Node, component, callback) {
        let eventHandler: cc.Component.EventHandler = new cc.Component.EventHandler()
        eventHandler.target = target
        eventHandler.component = component
        eventHandler.handler = callback
        let clickEvents = node.getComponent(cc.Button).clickEvents
        let isEvent = false
        for (let i = 0; i < clickEvents.length; i++) {
            const event: cc.Component.EventHandler = clickEvents[i];
            if (event) {
                if (event.component == component && event.target == target && event.handler == callback) {
                    isEvent = true
                }
            }
        }
        if (!isEvent) {
            clickEvents.push(eventHandler)
        } else {
            console.log("已经添加过相同的点击事件了");
        }
    }

    /**
     * 验证是否可以被点击 如果可以移除mask
     */
    verifyCanClick() {
        let children = GameMgr.instance._layout.children
        for (let index = 0; index < children.length; index++) {
            const first_ele: myNode = children[index];
            let first_x = first_ele.getPosition().x
            let first_y = first_ele.getPosition().y
            for (let j = index + 1; j < children.length; j++) {
                const second_ele: myNode = children[j];
                let second_x = second_ele.getPosition().x
                let second_y = second_ele.getPosition().y
                if ((Math.abs(second_x - first_x) < GameMgr.instance.cardWidth && Math.abs(second_y - first_y ) < GameMgr.instance.cardHeight && second_ele.index > first_ele.index)) {
                    if (!(first_ele.coverArr && first_ele.coverArr.length)) {
                        first_ele.coverArr = []
                    }
                    first_ele.coverArr.push(second_ele)
                }
            }
        }
        this.removeMask(children)

    }

    removeMask(children) {
        children.forEach((item: any) => {
            if (!item.coverArr || item.coverArr.length === 0) {
                let mask = item.getChildByName("mask")
                mask.active = false
            }
        })
        GameMgr.instance.renderCardArr = children
    }


    verifyCanRemoveMask() {
        GameMgr.instance.renderCardArr.forEach(item => {
            GameMgr.instance.hasClickedArr.forEach(single => {
                if (item.coverArr && item.coverArr.length && item.active) {
                    item.coverArr.forEach((cover, idx) => {
                        if (cover.index == single) {
                            item.coverArr.splice(idx, 1)
                        }
                    })
                }
            })
        })
        this.removeMask(GameMgr.instance.renderCardArr)
    }

    showToast(node,title,content,btntext,selfcallback){
        cc.loader.loadRes("toast/Toast", (err, toast) => {
            if (!err) {
                let toastNode: cc.Node = cc.instantiate(toast)
                toastNode.addComponent("Toast")
                node.addChild(toastNode)
                toastNode.setPosition(0, 0)
                toastNode.active = true
                toastNode.emit("fade-in", {
                    title, content, btntext, callback:()=>{
                        selfcallback(toastNode)
                    }
                })
            }
        })
    }

}
